import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ProfileForm extends HttpServlet {
  public void service(HttpServletRequest req,   HttpServletResponse response)   throws IOException,ServletException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();  
  Connection con = null;  
  Statement stmt = null;
  ResultSet rs = null;
  String uid = req.getParameter("userid");
String fname = req.getParameter("firstname");
 String lname = req.getParameter("lastname");
 String address = req.getParameter("address");
  String phone = req.getParameter("phone");
 String ccn = req.getParameter("ccn");
 String skym = req.getParameter("sky");
 String name= new String("");
	
	System.out.println("1");
  try {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con =DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");
  stmt = con.createStatement();
    	
 System.out.println("2");
  rs = stmt.executeQuery("SELECT * FROM db_user where user_id='"+uid+"'");
  while(rs.next()){
  name=rs.getString("user_id");
System.out.println("3");
                                  }

  rs.close();
  stmt.close();
 
 out.println("</table> </body> </html>");
  }
catch(Exception e){
  System.out.println(e);
  }
  Boolean isResponseCorrect = Boolean.FALSE;
System.out.println("4");
  if(!(uid.equals(name))){
  isResponseCorrect=true;
	
	try{	
	
	PreparedStatement pst = con.prepareStatement ( "insert into db_user values (?,?,?,?,?,?,?)");	
	//String sql = "insert into db_user values (?,?,?,?,?,?,?)";

	 pst.setString(1, uid);
  	pst.setString(2, fname);
  	pst.setString(3, lname);
 	 pst.setString(4, address);
 	 pst.setString(5, phone);
 	pst.setString(6, ccn);
	 pst.setString(7, skym);

System.out.println("5    "+"      "+uid+"    "+fname+"    "+lname+"    "+address+"     "+phone+"      "+ccn+"      "+skym);
 	 int numRowsChanged = pst.executeUpdate();
System.out.println("6      "+numRowsChanged);
	}catch(Exception e){ e.printStackTrace();}
	out.println("<html>");
	out.println("<body>");
	out.println("<table border=0 width=100%>");
	out.println("<tr>");
	out.println("<th> <b><font color='red'>Your Profile Has Been Created!</font><a href=main.html>Proceed</a></b> </th>");
	out.println("</tr>");
	//pst.close();
  }
  else{
  isResponseCorrect=false;
	out.println("<html>");
	out.println("<body>");
	out.println("<table border=1 width=100%>");
	out.println("<tr>");
	out.println("<th> <b><font color='red'>This user id is Not A Valid Name.Please try again!</font><a href=main.html>Proceed</a></b> </th>");
	out.println("</tr>");
  }
  }
}